<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 11:33
 */

namespace AppBundle\Task;

use AdminBundle\Entity\HuobiAmazingLog;

abstract class AbstractAlertModel implements TaskInterface
{
    private $cache_key = 'list:kline_amazing';
    private $container = null;

    abstract public function title();

    abstract public function name();

    abstract public function version();

    abstract public function prefix();


    public function __construct($container)
    {
        $this->container = $container;
    }

    public function isPause()
    {
        $em = $this->container->get('doctrine')->getManager();
        try {
            $repo = $em->getRepository('AdminBundle:HuobiCalcKline1min');
            return $repo->isPauseForMode($this->prefix());
        } finally {
            unset($repo);
        }
    }

    public function run($options = array())
    {
        $result = $this->modelCheck($options);
        if (empty($result)) {
            return null;
        }

        $huobiAmazingLog = new HuobiAmazingLog();

        $huobiAmazingLog->setVersion($this->version())
            ->setReportType($this->prefix())
            ->setCoin($options['coin'])
            ->setTimeStamp($options['time_stamp'])
            ->setCoinTime((new \DateTime())->setTimestamp($options['time_stamp']))
            ->setCoinOpen($options['open'])
            ->setCoinClose($options['close'])
            ->setCoinLow($options['low'])
            ->setCoinHigh($options['high'])
            ->setCoinAmount($options['amount'])
            ->setCoinVol($options['vol'])
            ->setCoinCount($options['count']);

        $em = $this->container->get('doctrine')->getManager();
        $em->persist($huobiAmazingLog);
        $em->flush();

        return $huobiAmazingLog->toApiJson();
//        return $huobiAmazingLog->toJson();
    }

    /**
     * 模型检查，是否出现信号
     * @param array $options
     * @return array|mixed
     */
    protected function modelCheck($options = array())
    {
        $length = isset($options['length']) ? $options['length'] : 1;
        $duration = isset($options['duration']) ? $options['duration'] : 0;
        $coin = isset($options['coin']) ? $options['coin'] : '';
        $timestamp = isset($options['time_stamp']) ? $options['time_stamp'] : time();

        $em = $this->container->get('doctrine')->getManager();

        try {
            $repo = $em->getRepository('AdminBundle:HuobiCalcKline1min');

            $result = call_user_func([$repo, $this->name()],
                $length, $duration, $coin, $timestamp
            );

        } catch (\Exception $e) {
            echo "model check error.";
        } finally {
            unset($repo);
        }

        return $result;
    }

    public function process($options = array())
    {
        if ($this->isPause()) {
            return [];
        }

        $length = isset($options['length']) ? $options['length'] : 1;
        $duration = isset($options['duration']) ? $options['duration'] : 2;
        $coin = isset($options['coin']) ? $options['coin'] : '';
        $timestamp = isset($options['time_stamp']) ? $options['time_stamp'] : time();

        $em = $this->container->get('doctrine')->getManager();

        try {
            $repo = $em->getRepository('AdminBundle:HuobiAmazingLog');

            $result = $repo->getAmazing($length, $duration, $coin, $timestamp, $this->version());

        } finally {
            unset($repo);
        }

        return $result;
    }

    /**
     * @return string
     */
    public function getCacheKey()
    {
        return $this->cache_key;
    }
}